/*
* @Author: Someu
* @Date:   2017-12-03 17:00:49
* @Last Modified by:   Someu
* @Last Modified time: 2017-12-03 18:25:07
*/
require('./index.css');
require('page/common/nav-simple/index.css');

var _mm     = require('util/mm.js');
var _user   = require('service/user-service.js');

var formError = {
    show : function(errMsg){
        $('.error-item').show().find('.err-msg').text(errMsg);
    },
    hide : function(){
        $('.error-item').hide().find('.err-msg').text('');
    }
};

var page = {
    data : {
        username    : '',
        question    : '',
        answer      : '',
        token       : ''
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    bindEvent : function(){
        var _this = this;
        $('#submit-username').click(function(){
            var username = $.trim($('#username').val());
            //用户名存在
            if(username){
                /*_user.getQuestion(username,function(res){
                    _this.data.username = username;
                    _this.data.question = res;
                    _this.loadStepQuestion();

                },function(errMsg){
                    formError(errMsg);
                })*/
                _this.loadStepQuestion();
            }
            //用户名不存在
            else{
                formError.show('请输入用户名');
            }
        });
        //输入密码提示答案点击
        $('#submit-question').click(function(){
            var answer = $.trim($('#answer').val());
            //密码提示答案非空
            if(answer){
                /*
                //验证密码提示答案
                _user.checkAnswer({
                    username : _this.data.username,
                    question : _this.data.question,
                    answer   : answer
                },function(res){
                    _this.data.answer   = answer;
                    _this.data.token    = res;
                    _this.loadStepPassword();
                },function(errMsg){
                    formError(errMsg);
                });*/
                _this.loadStepPassword();
            }
            //密码提示答案空
            else{
                formError.show('请输入密码提示答案');
            }
        });
        //输入新密码后点击
        $('#submit-password').click(function(){
            var password = $.trim($('#password').val());
            //密码提示答案非空
            if(password && password.length>=6){
                /*
                //重置新密码
                _user.resetPassword({
                    username        : _this.data.username,
                    passwordNew     : password,
                    forgetToken     : _this.data.token
                },function(res){
                    window.locaton.href = './result.html?type=password-reset';
                },function(errMsg){
                    formError.show(errMsg);
                });*/
                window.location.href = './result.html?type=password-reset';
            }
            //密码提示答案空
            else{
                formError.show('请输入不少于６位的新密码');
            }
        });
    },
    onLoad : function(){
        this.loadStepUsername();
    },
    /*加载输入用户名*/
    loadStepUsername : function(){
        $('.step-username').show();
    },
    /*加载输入密码提示答案*/
    loadStepQuestion : function(){
        //清除错误提示
        formError.hide();
        //容器切换
        $('.step-username').hide()
            .siblings('.step-question').show()
            .find('.question').text(this.data.question);

    },
    /*加载输入passwd*/
    loadStepPassword : function(){
        //清除错误提示
        formError.hide();
        //容器切换
        $('.step-question').hide()
            .siblings('.step-password').show();
    },
};

$(function(){
    page.init();
});